package com.library.weidget;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

import com.jtjrenren.android.taxi.passenger.R;

import java.util.Timer;
import java.util.TimerTask;

/**
 * Created by wdl on 2016/2/15.
 */
public class RadarProcessView extends View {
    private static final int REFRESH_JG = 1000; //时间读秒间隔 1秒
    private static final int REFRESH_MAX = 120; //120秒 等待时间

    public interface TimeListener {
        void timeOver();
    }


    private Paint cril_paint, yh_paint, jd_paint; //画笔对象的引用
    private Bitmap bitmap_scann; //扫描的圆弧
    private int inner_radius;

    private int process = 0;
    private float degrees = 0;
    private long lastProTime = 0;
    private TimeListener listener;

    private Timer timer;

    public void setListener(TimeListener listener) {
        this.listener = listener;
    }

    public RadarProcessView(Context context) {
        this(context, null);
    }

    public RadarProcessView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public RadarProcessView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        timer = new Timer();

        cril_paint = new Paint();
        cril_paint.setColor(getResources().getColor(R.color.radar_bg)); //设置颜色
        cril_paint.setAntiAlias(true);  //消除锯齿

        yh_paint = new Paint();
        yh_paint.setColor(getResources().getColor(R.color.radar_bg)); //设置圆环的颜色
        yh_paint.setStyle(Paint.Style.STROKE); //设置空心
        yh_paint.setStrokeWidth(4); //设置圆环的宽度
        yh_paint.setAntiAlias(true);  //消除锯齿

        jd_paint = new Paint();
        jd_paint.setColor(getResources().getColor(R.color.label_green));
        jd_paint.setStyle(Paint.Style.STROKE); //设置空心
        jd_paint.setStrokeWidth(4); //设置圆环的宽度
        jd_paint.setAntiAlias(true);  //消除锯齿

        bitmap_scann = BitmapFactory.decodeResource(getResources(), R.mipmap.def_scanning);

        startScan();
    }

    /**
     * 开启扫描记时
     */
    public void startScan() {
        process = 0;
        timer = null;
        timer = new Timer();

        timer.schedule(new myTask(), 0, 20);
    }

    public void stopScan() {
        if (timer != null)
            timer.cancel();
    }

    private long lastDrawTime=0;

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        inner_radius = getWidth() / 2 - 10;

        long start = System.currentTimeMillis();
        lastDrawTime=start;

        int center_x = getWidth() / 2;
        int center_y = getHeight() / 2;

        //绘制最里层的背景实心圆
        canvas.drawCircle(center_x, center_y, inner_radius, cril_paint); //画出圆


        //绘制扫描图片
        int nWidth = inner_radius * 2, nHeight = inner_radius * 2; //需要的高度和宽度
        int width = bitmap_scann.getWidth();
        int height = bitmap_scann.getHeight();
        float scaleWidth = nWidth / (float) width;
        float scaleHeight = nHeight / (float) height;
        Matrix matrix = new Matrix();
        matrix.postScale(scaleWidth, scaleHeight);
        matrix.postRotate(degrees, width / 2, height / 2);
        Bitmap bitmap = Bitmap.createBitmap(bitmap_scann, 0, 0, width,
                height, matrix, true);

        canvas.drawBitmap(bitmap, center_x - bitmap.getWidth() / 2, center_y - bitmap.getHeight() / 2, null);

        //绘制外圆环
        int out_radius = inner_radius + 8; //圆环的半径
        canvas.drawCircle(center_x, center_y, out_radius, yh_paint); //画出圆环

        //绘制进度
        if (System.currentTimeMillis() - lastProTime > REFRESH_JG) {
            lastProTime = System.currentTimeMillis();
            process++;
        }
        RectF oval = new RectF(center_x - out_radius, center_y - out_radius, center_x
                + out_radius, center_y + out_radius);  //用于定义的圆弧的形状和大小的界限
        canvas.drawArc(oval, 270, 360 * process / REFRESH_MAX, false, jd_paint);  //根据进度画圆弧

        if (process == REFRESH_MAX) {
            stopScan();
            if (listener != null)
                listener.timeOver();
        }

    }

    class myTask extends TimerTask {

        @Override
        public void run() {
            degrees = (degrees += 0.2) > 360 ? 0 : degrees;
            postInvalidate();
        }
    }

}
